// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  if (event.action == 'reserve') {
    let proArr = []
    event.list.forEach(id => {
      let pro = db.collection('classVenue').doc(id).update({
        data: {
          timeList: _.push(event.date)
        }
      })
      proArr.push(pro)
    })
    return await Promise.all(proArr)
  } else if (event.action == 'cancel') {
    return await db.collection('classVenue').doc(event.id).update({
      data: {
        timeList: _.pull(event.date)
      }
    })
  } else if (event.action == 'adminOrder') {
    return await db.collection('order')
      .where({
        status: '待使用',
        venueName: event.venueName,
        date: event.date
      })
      .get()
  }
}